package com.inei.cie.capitulovi;

import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.Carga_Trabajo_Activity;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloVIDao;
import com.inei.cie.entidades.P6_2;
import com.inei.cie.entidades.P6_2_4N;

public class CapituloVI_SeccionB_Espacio_Pedagogico_Activity extends Activity {

	public Spinner sp6B6;
	public Spinner sp6B7;
	public Spinner sp6B8;
	public Spinner sp6B9;
	public Spinner sp6B10;
	public EditText et6B7;
	public EditText et6B8;
	public EditText et6B9;
	public EditText et6B10;
	public TableLayout tl6Bespacio;
	public TableRow tr6B6;
	public TableRow tr6B7;
	public TableRow tr6B8;
	public TableRow tr6B9;
	public TableRow tr6B10;
	public TextView tvEdificacion;

	public P6_2 obj2;
	public CapituloVIDao cap6dao;
	public List<P6_2_4N> lObj2;
	public int Nro_Ed;
	public int Nro_Pred;
	public int P6_2_1;
	public int P6_2_2;
	public String id_local;
	public String edificacion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_vi_seccion_b_espacio_pedagogico);

		IniciarComponentes();

		sp6B6.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				switch (pos) {
				case 4:
					ocultarFilas();
					tl6Bespacio.addView(tr6B7,
							tl6Bespacio.indexOfChild(tr6B6) + 1);
					defaultSpinner();
					limpiarEditText();
					deshabilitarEditText();
					deshabilitarSpinner();
					sp6B7.setClickable(true);
					if (obj2.getP6_2_7() != 0) {
						if (obj2.getP6_2_7_O().length() != 0) {
							et6B7.setEnabled(true);
							et6B7.setText(obj2.getP6_2_7_O());
						}
						sp6B7.setSelection(obj2.getP6_2_7());
					}
					break;
				case 5:
					ocultarFilas();
					tl6Bespacio.addView(tr6B8,
							tl6Bespacio.indexOfChild(tr6B6) + 1);
					defaultSpinner();
					limpiarEditText();
					deshabilitarEditText();
					deshabilitarSpinner();
					sp6B8.setClickable(true);
					if (obj2.getP6_2_8() != 0) {
						sp6B8.setSelection(obj2.getP6_2_8());
						if (obj2.getP6_2_8_O().length() != 0) {
							et6B8.setEnabled(true);
							et6B8.setText(obj2.getP6_2_8_O());
						}
					}
					break;
				case 6:
					ocultarFilas();
					tl6Bespacio.addView(tr6B9,
							tl6Bespacio.indexOfChild(tr6B6) + 1);
					defaultSpinner();
					limpiarEditText();
					deshabilitarEditText();
					deshabilitarSpinner();
					sp6B9.setClickable(true);
					if (obj2.getP6_2_9() != 0) {
						sp6B9.setSelection(obj2.getP6_2_9());
						if (obj2.getP6_2_9_O().length() != 0) {
							et6B9.setEnabled(true);
							et6B9.setText(obj2.getP6_2_9_O());
						}
					}
					break;
				case 7:
					ocultarFilas();
					tl6Bespacio.addView(tr6B10,
							tl6Bespacio.indexOfChild(tr6B6) + 1);
					defaultSpinner();
					limpiarEditText();
					deshabilitarEditText();
					deshabilitarSpinner();
					sp6B10.setClickable(true);
					if (obj2.getP6_2_10() != 0) {
						sp6B10.setSelection(obj2.getP6_2_10());
						if (obj2.getP6_2_10_O().length() != 0) {
							et6B10.setEnabled(true);
							et6B10.setText(obj2.getP6_2_10_O());
						}
					}
					break;

				default:
					ocultarFilas();
					defaultSpinner();
					limpiarEditText();
					deshabilitarEditText();
					deshabilitarSpinner();
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});
		sp6B7.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				switch (pos) {
				case 7:
					deshabilitarEditText();
					et6B7.setEnabled(true);
					et6B7.setVisibility(View.VISIBLE);
					break;

				default:
					et6B7.setText("");
					deshabilitarEditText();
					et6B7.setVisibility(View.INVISIBLE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});
		sp6B8.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				switch (pos) {
				case 6:
					deshabilitarEditText();
					et6B8.setEnabled(true);
					et6B8.setVisibility(View.VISIBLE);
					break;

				default:
					et6B8.setText("");
					deshabilitarEditText();
					et6B8.setVisibility(View.INVISIBLE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});
		sp6B9.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				switch (pos) {
				case 17:
					deshabilitarEditText();
					et6B9.setEnabled(true);
					et6B9.setVisibility(View.VISIBLE);
					break;

				default:
					et6B9.setText("");
					deshabilitarEditText();
					et6B9.setVisibility(View.INVISIBLE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		sp6B10.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				switch (pos) {
				case 8:
					deshabilitarEditText();
					et6B10.setEnabled(true);
					et6B10.setVisibility(View.VISIBLE);
					break;

				default:
					et6B10.setText("");
					deshabilitarEditText();
					et6B10.setVisibility(View.INVISIBLE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		CargarDatos();
	}

	public void IniciarComponentes() {
		id_local = getIntent().getExtras().getString("id_local");
		Nro_Ed = getIntent().getExtras().getInt("Nro_Ed");
		P6_2_1 = getIntent().getExtras().getInt("P6_2_1");
		P6_2_2 = getIntent().getExtras().getInt("P6_2_2");
		Nro_Pred = getIntent().getExtras().getInt("Nro_Pred");
		edificacion = "Local : "
				+ id_local
				+ " - Predio : "
				+ (getIntent().getExtras().getInt("TITULO_PRED") < 10 ? "0"
						: "") + getIntent().getExtras().getInt("TITULO_PRED")
				+ " - Edificaci\u00f3n Nro. " + (Nro_Ed < 10 ? "0" : "")
				+ Nro_Ed;

		tvEdificacion = (TextView) findViewById(R.id.tvEdificacionNro);
		tvEdificacion.setText(edificacion);

		tl6Bespacio = (TableLayout) findViewById(R.id.tl6Bespacio);
		tr6B6 = (TableRow) findViewById(R.id.tr6B6);
		tr6B7 = (TableRow) findViewById(R.id.tr6B7);
		tr6B8 = (TableRow) findViewById(R.id.tr6B8);
		tr6B9 = (TableRow) findViewById(R.id.tr6B9);
		tr6B10 = (TableRow) findViewById(R.id.tr6B10);

		sp6B6 = (Spinner) findViewById(R.id.sp6B6);
		sp6B7 = (Spinner) findViewById(R.id.sp6B7);
		sp6B8 = (Spinner) findViewById(R.id.sp6B8);
		sp6B9 = (Spinner) findViewById(R.id.sp6B9);
		sp6B10 = (Spinner) findViewById(R.id.sp6B10);

		et6B7 = (EditText) findViewById(R.id.et6B7);
		et6B8 = (EditText) findViewById(R.id.et6B8);
		et6B9 = (EditText) findViewById(R.id.et6B9);
		et6B10 = (EditText) findViewById(R.id.et6B10);

		tl6Bespacio.removeView(tr6B7);
		tl6Bespacio.removeView(tr6B8);
		tl6Bespacio.removeView(tr6B9);
		tl6Bespacio.removeView(tr6B10);
	}

	public void CargarDatos() {
		cap6dao = new CapituloVIDao(getApplicationContext());
		List<Object> lista = cap6dao.obtenerAmbiente(id_local, Nro_Ed,
				Nro_Pred, P6_2_1, P6_2_2);
		obj2 = (P6_2) lista.get(0);
		lObj2 = (List<P6_2_4N>) lista.get(1);
		if (obj2 != null) {
			if (obj2.getP6_2_6() != 0) {
				sp6B6.setSelection(obj2.getP6_2_6());
			}
			if (obj2.getP6_2_7() != 0) {
				tl6Bespacio.addView(tr6B7, tl6Bespacio.indexOfChild(tr6B6) + 1);
				sp6B7.setSelection(obj2.getP6_2_7());
				et6B7.setText(obj2.getP6_2_7_O());
			}
			if (obj2.getP6_2_8() != 0) {
				tl6Bespacio.addView(tr6B8, tl6Bespacio.indexOfChild(tr6B6) + 1);
				sp6B8.setSelection(obj2.getP6_2_8());
				et6B8.setText(obj2.getP6_2_8_O());
			}
			if (obj2.getP6_2_9() != 0) {
				tl6Bespacio.addView(tr6B9, tl6Bespacio.indexOfChild(tr6B6) + 1);
				sp6B9.setSelection(obj2.getP6_2_9());
				et6B9.setText(obj2.getP6_2_9_O());
			}
			if (obj2.getP6_2_10() != 0) {
				tl6Bespacio
						.addView(tr6B10, tl6Bespacio.indexOfChild(tr6B6) + 1);
				sp6B10.setSelection(obj2.getP6_2_10());
				et6B10.setText(obj2.getP6_2_10_O());
			}
		}

	}

	public void GuardarDatos() {
		obj2.setP6_2_6(sp6B6.getSelectedItemPosition());
		obj2.setP6_2_7(sp6B7.getSelectedItemPosition());
		obj2.setP6_2_7_O(et6B7.getText().toString());
		obj2.setP6_2_8(sp6B8.getSelectedItemPosition());
		obj2.setP6_2_8_O(et6B8.getText().toString());
		obj2.setP6_2_9(sp6B9.getSelectedItemPosition());
		obj2.setP6_2_9_O(et6B9.getText().toString());
		obj2.setP6_2_10(sp6B10.getSelectedItemPosition());
		obj2.setP6_2_10_O(et6B10.getText().toString());
		cap6dao.actualizarSeccionB(obj2, lObj2);

		Intent irConservacion = new Intent(
				CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
				CapituloVI_SeccionB_Evaluacion_Del_Estado_De_Conservacion_Activity.class);
		irConservacion.putExtras(getIntent().getExtras());
		startActivity(irConservacion);

		finish();
	}

	public void deshabilitarSpinner() {
		sp6B7.setClickable(false);
		sp6B8.setClickable(false);
		sp6B9.setClickable(false);
		sp6B10.setClickable(false);
	}

	public void deshabilitarEditText() {
		et6B7.setEnabled(false);
		et6B8.setEnabled(false);
		et6B9.setEnabled(false);
		et6B10.setEnabled(false);
	}

	public void limpiarEditText() {
		et6B7.setText("");
		et6B8.setText("");
		et6B9.setText("");
		et6B10.setText("");
	}

	public void defaultSpinner() {
		sp6B7.setSelection(0);
		sp6B8.setSelection(0);
		sp6B9.setSelection(0);
		sp6B10.setSelection(0);
	}

	public void ocultarFilas() {
		tl6Bespacio.removeView(tr6B7);
		tl6Bespacio.removeView(tr6B8);
		tl6Bespacio.removeView(tr6B9);
		tl6Bespacio.removeView(tr6B10);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_capitulovi, menu);
		menu.getItem(0).setTitle("AMBIENTES POR PISO");
		menu.getItem(1).setTitle("SIGUIENTE");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir ?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnMarco:
			Intent irMarco = new Intent(this, Carga_Trabajo_Activity.class);
			irMarco.putExtras(getIntent().getExtras());
			startActivity(irMarco);
			finish();
			break;
		case R.id.mnIncio:
			Intent marco = new Intent(this,
					CapituloVI_SeccionB_Lista_Ambientes_Activity.class);
			marco.putExtras(getIntent().getExtras());
			startActivity(marco);
			finish();
			break;
		case R.id.mnSiguiente:
			int p6_6 = sp6B6.getSelectedItemPosition();
			int p6_7 = sp6B7.getSelectedItemPosition();
			int p6_7_o = et6B7.getText().toString().trim().length();
			int p6_8 = sp6B8.getSelectedItemPosition();
			int p6_8_o = et6B8.getText().toString().trim().length();
			int p6_9 = sp6B9.getSelectedItemPosition();
			int p6_9_o = et6B9.getText().toString().trim().length();
			int p6_10 = sp6B10.getSelectedItemPosition();
			int p6_10_o = et6B10.getText().toString().trim().length();

			if (p6_6 == 0) {
				Toast.makeText(
						CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
						"Debe seleccionar una opci\u00f3n en la pregunta 6.",
						Toast.LENGTH_SHORT).show();
				break;
			}
			if (p6_6 == 4) {
				if (p6_7 == 0) {
					Toast.makeText(
							CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 7.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_7==7 &&  p6_7_o == 0) {
					Toast.makeText(
							CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
							"Debe especificar otro tipo de aula especial en la pregunta 7.",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
			if (p6_6 == 5) {
				if (p6_8 == 0) {
					Toast.makeText(
							CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 8.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_8==6 && p6_8_o == 0) {
					Toast.makeText(
							CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
							"Debe especificar otro tipo de laboratorio en la pregunta 8.",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
			if (p6_6 == 6) {
				if (p6_9 == 0) {
					Toast.makeText(
							CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 9.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_9 == 17 && p6_9_o == 0) {
					Toast.makeText(
							CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
							"Debe especificar otro tipo de taller ligero en la pregunta 9.",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
			if (p6_6 == 7) {
				if (p6_10 == 0) {
					Toast.makeText(
							CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
							"Debe seleccionar una opci\u00f3n en la pregunta 10.",
							Toast.LENGTH_SHORT).show();
					break;
				}
				if (p6_10 == 8 && p6_10_o == 0) {
					Toast.makeText(
							CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
							"Debe especificar otro tipo de taller semi-pesado en la pregunta 10.",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}

			GuardarDatos();
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(CapituloVI_SeccionB_Espacio_Pedagogico_Activity.this,
					CapituloIX_Activity.class);
			// b.putBoolean("pregunta", true);
			foto.putExtras(getIntent().getExtras());
			foto.putExtra("pregunta", true);
			startActivity(foto);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent irPredio = new Intent(this,
				CapituloVI_SeccionB_Lista_Ambientes_Activity.class);
		irPredio.putExtras(getIntent().getExtras());
		startActivity(irPredio);
		finish();
	}
}
